package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

/**
 * Created by Administrator on 2017/2/27.
 */
@Repository
public class UserDapImpl implements UserDao {
private SessionFactory sessionFactory;
//    public void init(){
//        users[0]=new User(1,"lkj","123");
//        users[1]=new User(2,"lh","123");
//        users[2]=new User(3,"qqq","123");
//    }
    @Override
    public User findByUsernamePassword(String username, String password) {
//       /*初始化几个数据。从里面找登录的用户*/
//       this.init();
////       遍历
//       for (User u:users){
//           if (u.getUsername().equals(username)&&u.getPassword().equals(password)){
//               return u;
//           }
//       }

//        从数据库中查
        Session session=sessionFactory.openSession();
        Transaction transaction=session.beginTransaction();
        User user =(User)session.createQuery(
                "from User u where u.password=? and u.username=?")
                .setParameter(0,password)
                .setParameter(1,username)
                .uniqueResult();
        transaction.commit();
        session.close();
        return user;
    }
}
